public class Move {
	public static char cNull = '\u0000';
	private char color;
	private char piece;
	private int previousColumn;
	private int previousRow;
	private int newColumn;
	private int newRow;
	private char castleType = cNull;
	private State newState;
	private char newPiece = cNull; // If a Pawn cashes in
	public static int board_dimension = 5;
	
	public Move(char color, char piece, int previousColumn, int previousRow, int newColumn, int newRow, State currentState){
		moveConstructor(color, piece, previousColumn, previousRow, newColumn, newRow, currentState);
	}
	
	public Move(char color, char piece, int previousColumn, int previousRow, int newColumn, int newRow, State currentState, char newPiece, boolean isEmpazant, char castleType){
		this.newPiece = newPiece;
		moveConstructor(color, piece, previousColumn, previousRow, newColumn, newRow, currentState);
		if (this.newPiece != cNull){
			this.newState.changePiece(this.newColumn, this.newRow, this.newPiece);
		}
		else if (isEmpazant==true){
			this.newState.movePiece(this.newColumn, this.previousRow, -1,-1); // Remove the piece from the board
		}
		this.castleType = castleType;
		
		if (this.castleType!=cNull){
			int newRookCol, oldRookCol, rookRow;
			if (this.color=='W'){
				rookRow = 0;
			}
			else {
				rookRow = board_dimension-1;
			}
			if (this.castleType=='Q'){
				newRookCol = 3;
				oldRookCol = 0;
			}
			else {
				newRookCol = 5;
				oldRookCol = board_dimension-1;
			}
			this.newState.movePiece(oldRookCol, rookRow, newRookCol,rookRow); 
		}
	}
	
	private void moveConstructor(char color, char piece, int previousColumn, int previousRow, int newColumn, int newRow, State currentState){
		this.color = color;
		this.piece = piece;
		this.previousColumn = previousColumn;
		this.previousRow = previousRow;
		this.newColumn = newColumn;
		this.newRow = newRow;
		this.newState = currentState.clone();
		
		boolean isEmpazantable = false;
		Piece evalPiece;
		
		this.newState.movePiece(this.previousColumn, this.previousRow, this.newColumn, this.newRow);
		if (this.color=='W'){
			this.newState.setTurn('B');	
		}
		else{
			this.newState.setTurn('W');
		}
		if(this.piece=='P'&&Math.abs(this.newRow-this.previousRow)==2){ // This pawn moved two squares. If an opponents pawn is next to it is is empazantable
			
			if (newColumn-1>-1 &&
				this.newState.getBoard().hasPiece(newRow, newColumn-1) &&
				this.newState.getBoard().isColor(newRow, newColumn-1,this.newState.getTurn()) &&
				this.newState.getBoard().isCharacter(newRow,newColumn-1,'P')
				){
				isEmpazantable = true;
				}
			if (newColumn+1<board_dimension &&
				this.newState.getBoard().hasPiece(newRow, newColumn+1) &&
				this.newState.getBoard().isColor(newRow, newColumn+1,this.newState.getTurn()) &&
				this.newState.getBoard().isCharacter(newRow,newColumn+1,'P')
				){
				isEmpazantable = true;
				}
			if (isEmpazantable){
				this.newState.setEmpazantablePawn(this.newColumn, this.newRow);
			}
		}
		
		if (piece=='K'){
			this.newState.unsetCastleAvailability(this.color,'K');
			this.newState.unsetCastleAvailability(this.color,'Q');
		}
		// If rook is taken, castling on that side is out
		if (newColumn==0 && newRow==0){
			this.newState.unsetCastleAvailability('W','Q');
		}
		else if (newColumn==board_dimension-1 && newRow==0){
			this.newState.unsetCastleAvailability('W','K');
		}
		else if (newColumn==0 && newRow==board_dimension-1){
			this.newState.unsetCastleAvailability('B','Q');
		}
		else if (newColumn==board_dimension-1 && newRow==board_dimension-1){
			this.newState.unsetCastleAvailability('B','K');
		}
		
		if (piece=='R'){
			if (((this.previousRow==0 && this.color=='W') || (this.previousRow==board_dimension-1 && this.color=='B'))&& this.previousColumn==0){
				this.newState.unsetCastleAvailability(this.color,'Q');
			}
			else if (((this.previousRow==0 && this.color=='W') || (this.previousRow==board_dimension-1 && this.color=='B'))&& this.previousColumn==board_dimension-1){
				this.newState.unsetCastleAvailability(this.color,'K');
			}
		}
		
	}
	
	public boolean isQueenSideCastleBlankSquare(){
		if (this.piece=='K'){
			if ((this.newRow==0 || this.newRow==board_dimension-1) && this.newColumn==3){
				return true;
			}
		}
		return false;
	}
	
	public boolean isKingSideCastleBlankSquare(){
		if (this.piece=='K'){
			if ((this.newRow==0 || this.newRow==board_dimension-1) && this.newColumn==5){
				return true;
			}
		}
		return false;
	}
	
	public char getCastleType(){
		return this.castleType;
	}
	
	public int getColumn(){
		return this.newColumn;
	}
	
	public int getRow(){
		return this.newRow;
	}
	
	public State getState(){
		return this.newState;
	}
	
	public String getMove(){
		String move = Character.toString((char)(this.previousColumn+97))+Integer.toString(this.previousRow+1)+"-"+Character.toString((char)(this.newColumn+97))+Integer.toString(this.newRow+1);
		if (this.piece!='P'){
			move = this.piece + move;
		}
		if (newPiece != cNull){
			move = move + "(" +this.newPiece + ")";
		}
		if (castleType=='K'){
			move = "O-O";
		}
		if (castleType=='Q'){
			move = "O-O-O";
		}
		return move; 
	}
}